﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZSGC.Models;

namespace ZSGC.Controllers
{
    public class TaskController : Controller
    {
        //数据上下文
        Model1 model = new Model1();


        //显示列表
        public ActionResult Index()
        {
            var list = model.Task.ToList();
            return View(list);
        }

        //添加
        public ActionResult Create()
        {
            return View();
        }


        [HttpPost]
        public ActionResult Create(Task task)
        {
            if (ModelState.IsValid)
            {
                //与用户交互的属性，直接使用传递的数据
                //特殊处理:未与用户交互，且默认值无法满足要求数据库的属性必须重新赋值(数据库的默认值)
                task.AddTime = DateTime.Now;
                task.TaskState = 1;
                task.FinishedTime = null;


                model.Task.Add(task);
                var res =  model.SaveChanges();
                if (res>0)
                {
                    return RedirectToAction("Index");
                }
            }
            return View(task);
            
        }

        //修改
        public ActionResult Edit(int? id)
        {
            if (id==null)
            {
                return RedirectToAction("Index");
            }
            var task = model.Task.FirstOrDefault(p =>p.TaskID==id);
            if (task==null)
            {
                return RedirectToAction("Index");
            }

            return View(task);
        }

        [HttpPost]
        public ActionResult Edit(Task task)
        {

            if (ModelState.IsValid)
            {
                var temp = model.Task.FirstOrDefault(p => p.TaskID == task.TaskID);
                if (temp == null)
                {
                    return RedirectToAction("Index");

                }

                temp.Content = task.Content;
                temp.FinishedTime = task.FinishedTime;
                temp.TaskPriority = task.TaskPriority;
                temp.TaskState = task.TaskState;
                temp.uid = task.uid;
                var res = model.SaveChanges();
                if (res > 0)
                {
                    return RedirectToAction("Index");
                }
              
            }

            return View(task);
        }

        //详情
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return RedirectToAction("Index");
            }
            var task = model.Task.FirstOrDefault(p => p.TaskID == id);
            if (task == null)
            {
                return RedirectToAction("Index");
            }

            return View(task);
           
        }


        //删除
        [HttpPost]
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return Json(false);
            }
            var task = model.Task.FirstOrDefault(p => p.TaskID == id);
            if (task == null)
            {
                return Json(false);
            }

           model.Task.Remove(task);
            var res = model.SaveChanges();
            return Json(res > 0);
            
          
        }


    }
}